package com.tiny.mirror.service.product.domain.app.service;

import com.tiny.mirror.basic.db.domain.Aggregate;
import com.tiny.mirror.basic.db.domain.AggregateFactory;
import com.tiny.mirror.service.product.domain.app.App;
import com.tiny.mirror.service.product.domain.app.repository.AppRepository;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @Author: liujsh
 * @Description:
 * @Date: Create in 2023/9/25
 */
@Service
public class AppDomainService {

  @Resource
  private AppRepository appRepository;

  public Long create(App app) {
    Aggregate<App> appAggregate = AggregateFactory.createEmptyAggregate();
    appAggregate.superAdd(app);
    return appRepository.save(appAggregate).getRoot().getId();
  }

  public void update(Long id, App app) {
    Aggregate<App> appAggregate = appRepository.findById(id);
    appAggregate.getRoot().edit(app);
    appRepository.save(appAggregate);
  }

  public void delete(Long id) {
    Aggregate<App> appAggregate = appRepository.findById(id);
    appAggregate.getRoot().deletePlatformList();
    appAggregate.delete();
    appRepository.save(appAggregate);
  }
}
